package com.qingxin.sys.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.entity.SysRole;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysPermissionService;
import com.qingxin.sys.service.SysRoleRightsService;
import com.qingxin.sys.service.SysRoleService;
import com.qingxin.sys.utils.StringUtils;

/**
 * 角色管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/role")
public class RoleAction extends BaseAction {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysPermissionService sysPermissionService;
	
	@Autowired
	private SysRoleRightsService sysRoleRightsService;
	
	
	/**
	 * 角色列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "sys/role/role_list";
	}
	
	
	/**
	 * 列表数据
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String groupid, String search, PageParam pageParam, HttpServletRequest request){
		return sysRoleService.list(groupid, search, getLoginUser(request), pageParam, request);
	}
	
	
	/**
	 * 添加角色
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("groupid", leftdata);
		return "sys/role/role_add";
	}
	
	
	/**
	 * 保存角色信息
	 * @param role
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid SysRole role, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = sysRoleService.add(role, getLoginUser(request), request);
		return info;
	}
	

	
	/**
	 * 修改角色信息
	 * @param modelMap
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String roleid){
		SysRole role = sysRoleService.get(roleid);
		model.put("role", role);
		return "sys/role/role_edit";
	}
	
	
	/**
	 * 保存修改的角色信息
	 * 
	 * @param role
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid SysRole role, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = sysRoleService.update(role, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看角色详细信息
	 * 
	 * @param modelMap
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String roleid){
		SysRole role = sysRoleService.get(roleid);
		model.put("role", role);
		return "sys/role/role_view";
	}
	
	
	/**
	 * 删除角色信息
	 * @param roleid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] roleid, HttpServletRequest request){
		if(roleid==null || roleid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return sysRoleService.delete(roleid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断角色名称是否被使用
	 * @param role_name
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String role_name, String roleid, String groupid){
		boolean existed = sysRoleService.isNameDuplicate(role_name, roleid, groupid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 设置角色操作权限
	 * 
	 * @param model
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value = "/setRoleRights.do", method = RequestMethod.GET)
	public String setRoleRights(ModelMap model, String roleid) {
		model.put("roleid", roleid);
		return "sys/role/setRoleRights";
	}
	
	
	/**
	 * 获取操作权限树
	 * @param roleid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPermissions.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getPermissions(String roleid, HttpServletRequest request){
		SysRole role = sysRoleService.get(roleid);
		if(role == null){
			return new ArrayList<>();
		}
		return sysPermissionService.getRoleRightsPermissions(roleid, getLoginUser(request));
	}
	
	
	/**
	 * 保存指定角色的操作权限
	 * @param roleid
	 * @param perid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/setRoleRights.json", method = RequestMethod.POST)
	@ResponseBody
	public Object setRoleRights(String roleid, String[] perid, HttpServletRequest request) {
		if(StringUtils.isEmpty(roleid)) {
			return new SysError("参数错误.");
		}
		
		return sysRoleRightsService.saveRoleRights(roleid, perid, getLoginUser(request));
	}
	
	
	
	
}
